/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Wed 08 Jul 2020 12:15:12 PM CST
 * Destription:
 */

#include <acl-lib/acl/lib_acl.h>
#include <acl-lib/acl_cpp/lib_acl.hpp>
#include <acl-lib/acl_cpp/stdlib/json.hpp>

std::string data("{\"type\":2,\"key\":\"/a/b/c.txt\",\"ips\": [{\"v4\": \"1.1.1.1\",\"port\": 80},{\"v6\": \"CDCD:910A:2222:5498:8475:1111:3900:2020\",\"port\": 80},{\"v6\": \"CDCD:910A:2222:5498:8475:1111:3900:2022\",\"port\": 80}],\"zone\":\"shanghai\"}");
//std::string data("{\"type\":2,\"key\":\"/a/b/c.txt\",\"ips\": [{\"v6\": \"CDCD:910A:2222:5498:8475:1111:3900:2022\",\"port\": 80}],\"zone\":\"shanghai\"}");

typedef struct reg_ip {
    bool v4_;                //是否是v4
    std::string ip_;
    unsigned short port_;    //端口号
    reg_ip() {
        v4_ = true;
        ip_ = "";
        port_ = 0;
    }
} reg_ip;

typedef struct reg_obj {
    int type_;              //类型
    std::string key_;       //数据key
    std::vector<reg_ip>ips_;
    std::string zone_;      //区域
} reg_obj;


void parse_one(acl::json_node * nd,
        std::vector<reg_ip> & ips)
{
    reg_ip ip;
    bool bip = false;
    acl::json_node* nodeData = nd->first_child();
    while(NULL!=nodeData) {
        const char * name = nodeData->tag_name();
        if(NULL==name) {
            nodeData = nd->next_child();
            continue;
        }
        if(0==strcasecmp(name, "v4")) {
            if(nodeData->get_text()) {
                ip.ip_ = nodeData->get_text();
                ip.v4_ = true;
            }
        } else if(0==strcasecmp(name, "v6")) {
            if(nodeData->get_text()) {
                ip.ip_ = nodeData->get_text();
                ip.v4_ = false;
            }
        } else if(0==strcasecmp(name, "port")) {
            ip.port_ = (int) *nodeData->get_int64();
        }
        nodeData = nd->next_child();
    }
    if(false == ip.ip_.empty()) {
        ips.push_back(ip);
    }
}

reg_obj * parse_json_body() {
    reg_obj * obj =  new reg_obj();
    acl::json json(data.c_str());
    acl::string buf = json.to_string();
    printf("buf_json=%s\n", buf.c_str());
    if(1) {
        acl::json_node * node = json.first_node();
        while(node) {
            const char * name = node->tag_name();
            if(NULL==name) {
                node = json.next_node();
                continue;
            }
            if(0==strcasecmp(name, "type")) {
                obj->type_ = (int)*node->get_int64();
            } else if(0==strcasecmp(name, "key")) {
                obj->key_ = node->get_string();
            } else if(0==strcasecmp(name, "zone")) {
                obj->zone_ = node->get_string();
            } else if(0==strcasecmp(name, "ips")) {
                acl::json_node* nodeData = node->get_obj();
                if(NULL==nodeData || false==nodeData->is_array()){
                    delete obj;
                    return NULL;
                }
                acl::json_node* tmp = nodeData->first_child();
                while(tmp) {
                    parse_one(tmp, obj->ips_);
                    tmp = nodeData->next_child();
                }
            }
            node = json.next_node();
        }
    }
    return obj;
}

static long long NowMs() {
    struct timeval tm;
    gettimeofday(&tm, NULL);
    long long ret = tm.tv_sec * 1000 + tm.tv_usec / 1000;
    return ret;
}

void test_parse() {
    const char * data = "{\"add\": [{\"uri\":\"/videos/v0/20200420/24/32/fa1177a322a276622d494c1255c0c5f1.mp4\"}]}";
    acl::json json(data);
    acl::json_node * node = json.first_node();
    while(node) {
        const char * name = node->tag_name();
        if(NULL==name) {
            node = json.next_node();
            continue;
        } else {
            printf("name=%s\n",name);
            if(node->get_obj()) {
                node = node->get_obj();
            }
            printf("node tye=%s, name=%s\n", node->get_type(), node->tag_name());
        }
        node = json.next_node();
    }

}

int main() {
    test_parse();
    return 0;
    printf("date = %lld\n", NowMs());
    int a = 256;
    unsigned char b = a;
    int c = 0;
    memcpy(&c, &b, 1);
    printf("c=%d\n", c);
    //reg_obj * obj = parse_json_body();
    //if(obj) {
    //    printf("type=%d, key=%s, zone=%s\n", obj->type_, obj->key_.c_str(), obj->zone_.c_str());
    //    acl::string ip_info;
    //    for(size_t i=0; i<obj->ips_.size(); ++i) {
    //        printf("    ip[%d]=%s:%u, ipv4=%d\n", i, obj->ips_[i].ip_.c_str(), obj->ips_[i].port_, obj->ips_[i].v4_);
    //    }
    //    delete obj;
    //}
    //return 0;
}
